package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.NewEventConstant;
import com.tbit.uqbike.client.dao.core.InvitationJoinDao;
import com.tbit.uqbike.client.dao.log.UserRewardLogDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: chen
 * @Description: 用户邀请奖励记录
 * @Date: 创建于 14:46 2022/5/11
 */
@Service
public class UserRewardLogServiceImpl implements UserRewardLogService {

    @Autowired
    private UserRewardLogDao userRewardLogDao;
    @Autowired
    private UserInvitationService userInvitationService;
    @Autowired
    private EvenJoinDetailService evenJoinDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private InvitationJoinDao invitationJoinDao;
    @Autowired
    private ActiveStatService activeStatService;

    @Override
    public List<UserRewardLog> getByUserId(Integer userId, Integer eventId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("eventId", eventId);
        return userRewardLogDao.getByUserId(map);
    }

    @Override
    public Integer getUserCount(Integer userId, Integer eventId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("eventId", eventId);
        return userRewardLogDao.getUserCount(map);
    }

    @Override
    public void add(UserRewardLog userRewardLog) {
        userRewardLog.setAddTime(new Date());
        userRewardLogDao.add(userRewardLog);
    }

    @Override
    public List<UserRewardLog> getByCode(String code, Integer userId) {
        UserInvitation userInvitation = userInvitationService.getByCode(code);
        if (userInvitation == null) {
            throw new BaseException("-180002", "该邀请码不存在");
        }
        /**只能查看自己的记录*/
        if (!userInvitation.getUserId().equals(userId)) {
            return null;
        }
        return getByUserId(userInvitation.getUserId(), userInvitation.getEventId());
    }

    @Override
    public void editName(String inviteName, Integer userId) {
        UserRewardLog userRewardLog = new UserRewardLog();
        userRewardLog.setInviteName(inviteName);
        userRewardLog.setUserId(userId);
        userRewardLogDao.editName(userRewardLog);
        /**兼容实名认证性别分布*/
        List<InvitationJoin> invitationJoins = invitationJoinDao.getByUserId(userId);
        if (invitationJoins.size() == 0) {
            return;
        }
        List<Integer> list = new ArrayList<>();
        if (invitationJoins.size() > 0) {
            for (InvitationJoin in : invitationJoins) {
                if (!list.contains(in.getEventId())) {
                    list.add(in.getEventId());
                }
            }
        }

        /**添加用户性别等统计*/
        User user = userService.getByUserId(userId);
        for (Integer eventId : list) {
            ActiveStat activeStat = activeStatService.getByUserId(user.getPhone(), eventId);
            if (activeStat.getShare() > 0) {
                evenJoinDetailService.add(eventId, NewEventConstant.EVENT_TYPE_SHARE, user, 0);
            }
            if (activeStat.getWatch() > 0) {
                evenJoinDetailService.add(eventId, NewEventConstant.EVENT_TYPE_WATCH, user, 0);
            }
            if (activeStat.getReceive() > 0) {
                evenJoinDetailService.add(eventId, NewEventConstant.EVENT_TYPE_RECEIVE, user, 0);
            }
            if (activeStat.getFinish() > 0) {
                evenJoinDetailService.add(eventId, NewEventConstant.EVENT_TYPE_FINISH, user, 0);
            }
        }

    }


}
